using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Character.Jarily {

    [RequireComponent(typeof(Rigidbody2D))]
    public class Move : MonoBehaviour {
        [SerializeField]
        [Range(0, 10)]
        float m_Velocity = 0f;
        //float j_High = 0f;

        [SerializeField]
        [Range(0, 5)]
        float gravity = 0f;
        float velocityScale = 0f;

        internal bool CanMove { get; set; }
        public bool Walk = false;
        internal bool Jump { get; private set; }

        [SerializeField]
        //[Range(0, 10)]
        //float Damping = 0f;
        int Ground = 0;

        Rigidbody2D rigi = null;

        private void Awake() {
            rigi = GetComponent<Rigidbody2D>();
            m_collider = GetComponent<Collider2D>();
            gravity *= 2 * Physics2D.gravity.magnitude;
            Ground = LayerMask.NameToLayer("Ground");
            CanMove = true;
            Walk = false;
        }
        private void FixedUpdate() {
            if (CanMove) {
                if (!Walk) {
                    attenuateVelocity();
                    Debug.Log("attenuate");
                } else if (Walk) {
                    MovePosition(direction);
                    Debug.Log("Move");
                }
            } else {
                rigi.velocity = new Vector2(0, 0);
                rigi.bodyType = RigidbodyType2D.Static;
            }
        }

        internal bool direction { get; private set; }

        internal void SetMove(bool right) {
            direction = right;
        }

        internal void MovePosition(bool right) {
            rigi.velocity = new Vector2(GetVelocity(right), rigi.velocity.y);
            Walk = true;
        }

        //移速衰减
        void attenuateVelocity() {
            velocityScale = rigi.velocity.x / m_Velocity;
            //if (velocityScale < 0)
            //    if (velocityScale + Time.deltaTime > 0)
            //        velocityScale = 0;
            //    else
            //        velocityScale += Time.deltaTime * Damping;
            //else if (velocityScale > 0)
            //    if (velocityScale - Time.deltaTime < 0)
            //        velocityScale = 0;
            //    else
            //        velocityScale -= Time.deltaTime * Damping;
        }

        //获取速度
        float GetVelocity(bool right) {
            if (right)
                velocityScale = (velocityScale + Time.fixedDeltaTime * 1.0f) < 1 ? velocityScale + Time.fixedDeltaTime * 1.0f : 1;
            else
                velocityScale = (velocityScale - Time.fixedDeltaTime * 1.0f) > -1 ? velocityScale - Time.fixedDeltaTime * 1.0f : -1;
            return m_Velocity * velocityScale;
        }

        protected void JumpPosition(float high) {
            float x = Mathf.Sqrt(high * gravity);
            rigi.velocity = new Vector2(rigi.velocity.x, x);
        }

        Collider2D m_collider;

        private void OnCollisionStay2D(Collision2D collision) {
            if (collision.gameObject.layer == Ground) {
                ContactPoint2D[] point = collision.contacts;
                foreach (ContactPoint2D x in point)
                    if (x.normal.y >= 0.5f)
                        Jump = true;
            }
        }
        private void OnCollisionExit2D(Collision2D collision) {
            if (collision.gameObject.layer == Ground) {
                Jump = false;
            }
        }
    }
}


